


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > PlayerCollisions</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.level</a>
</div>

<h1>Coverage Summary for Class: PlayerCollisions (nl.tudelft.jpacman.level)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">PlayerCollisions</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/7)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/21)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.level;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import nl.tudelft.jpacman.board.Unit;
<i class="no-highlight">4</i>&nbsp;import nl.tudelft.jpacman.npc.Ghost;
<i class="no-highlight">5</i>&nbsp;import nl.tudelft.jpacman.points.PointCalculator;
<i class="no-highlight">6</i>&nbsp;
<i class="no-highlight">7</i>&nbsp;/**
<i class="no-highlight">8</i>&nbsp; * A simple implementation of a collision map for the JPacman player.
<i class="no-highlight">9</i>&nbsp; * &lt;p&gt;
<i class="no-highlight">10</i>&nbsp; * It uses a number of instanceof checks to implement the multiple dispatch for the 
<i class="no-highlight">11</i>&nbsp; * collisionmap. For more realistic collision maps, this approach will not scale,
<i class="no-highlight">12</i>&nbsp; * and the recommended approach is to use a {@link CollisionInteractionMap}.
<i class="no-highlight">13</i>&nbsp; *
<i class="no-highlight">14</i>&nbsp; * @author Arie van Deursen, 2014
<i class="no-highlight">15</i>&nbsp; *
<i class="no-highlight">16</i>&nbsp; */
<i class="no-highlight">17</i>&nbsp;
<i class="no-highlight">18</i>&nbsp;public class PlayerCollisions implements CollisionMap {
<i class="no-highlight">19</i>&nbsp;
<i class="no-highlight">20</i>&nbsp;    private PointCalculator pointCalculator;
<i class="no-highlight">21</i>&nbsp;
<i class="no-highlight">22</i>&nbsp;    /**
<i class="no-highlight">23</i>&nbsp;     * Create a simple player-based collision map, informing the
<i class="no-highlight">24</i>&nbsp;     * point calculator about points to be added.
<i class="no-highlight">25</i>&nbsp;     *
<i class="no-highlight">26</i>&nbsp;     * @param pointCalculator
<i class="no-highlight">27</i>&nbsp;     *             Strategy for calculating points.
<i class="no-highlight">28</i>&nbsp;     */
<b class="nc"><i class="no-highlight">29</i>&nbsp;    public PlayerCollisions(PointCalculator pointCalculator) {</b>
<b class="nc"><i class="no-highlight">30</i>&nbsp;        this.pointCalculator = pointCalculator;</b>
<i class="no-highlight">31</i>&nbsp;    }
<i class="no-highlight">32</i>&nbsp;
<i class="no-highlight">33</i>&nbsp;    @Override
<i class="no-highlight">34</i>&nbsp;    public void collide(Unit mover, Unit collidedOn) {
<b class="nc"><i class="no-highlight">35</i>&nbsp;        if (mover instanceof Player) {</b>
<b class="nc"><i class="no-highlight">36</i>&nbsp;            playerColliding((Player) mover, collidedOn);</b>
<i class="no-highlight">37</i>&nbsp;        }
<b class="nc"><i class="no-highlight">38</i>&nbsp;        else if (mover instanceof Ghost) {</b>
<b class="nc"><i class="no-highlight">39</i>&nbsp;            ghostColliding((Ghost) mover, collidedOn);</b>
<i class="no-highlight">40</i>&nbsp;        }
<b class="nc"><i class="no-highlight">41</i>&nbsp;        else if (mover instanceof Pellet) {</b>
<b class="nc"><i class="no-highlight">42</i>&nbsp;            pelletColliding((Pellet) mover, collidedOn);</b>
<i class="no-highlight">43</i>&nbsp;        }
<i class="no-highlight">44</i>&nbsp;    }
<i class="no-highlight">45</i>&nbsp;
<i class="no-highlight">46</i>&nbsp;    private void playerColliding(Player player, Unit collidedOn) {
<b class="nc"><i class="no-highlight">47</i>&nbsp;        if (collidedOn instanceof Ghost) {</b>
<b class="nc"><i class="no-highlight">48</i>&nbsp;            playerVersusGhost(player, (Ghost) collidedOn);</b>
<i class="no-highlight">49</i>&nbsp;        }
<b class="nc"><i class="no-highlight">50</i>&nbsp;        if (collidedOn instanceof Pellet) {</b>
<b class="nc"><i class="no-highlight">51</i>&nbsp;            playerVersusPellet(player, (Pellet) collidedOn);</b>
<i class="no-highlight">52</i>&nbsp;        }
<i class="no-highlight">53</i>&nbsp;    }
<i class="no-highlight">54</i>&nbsp;
<i class="no-highlight">55</i>&nbsp;    private void ghostColliding(Ghost ghost, Unit collidedOn) {
<b class="nc"><i class="no-highlight">56</i>&nbsp;        if (collidedOn instanceof Player) {</b>
<b class="nc"><i class="no-highlight">57</i>&nbsp;            playerVersusGhost((Player) collidedOn, ghost);</b>
<i class="no-highlight">58</i>&nbsp;        }
<i class="no-highlight">59</i>&nbsp;    }
<i class="no-highlight">60</i>&nbsp;
<i class="no-highlight">61</i>&nbsp;    private void pelletColliding(Pellet pellet, Unit collidedOn) {
<b class="nc"><i class="no-highlight">62</i>&nbsp;        if (collidedOn instanceof Player) {</b>
<b class="nc"><i class="no-highlight">63</i>&nbsp;            playerVersusPellet((Player) collidedOn, pellet);</b>
<i class="no-highlight">64</i>&nbsp;        }
<i class="no-highlight">65</i>&nbsp;    }
<i class="no-highlight">66</i>&nbsp;
<i class="no-highlight">67</i>&nbsp;
<i class="no-highlight">68</i>&nbsp;    /**
<i class="no-highlight">69</i>&nbsp;     * Actual case of player bumping into ghost or vice versa.
<i class="no-highlight">70</i>&nbsp;     *
<i class="no-highlight">71</i>&nbsp;     * @param player
<i class="no-highlight">72</i>&nbsp;     *          The player involved in the collision.
<i class="no-highlight">73</i>&nbsp;     * @param ghost
<i class="no-highlight">74</i>&nbsp;     *          The ghost involved in the collision.
<i class="no-highlight">75</i>&nbsp;     */
<i class="no-highlight">76</i>&nbsp;    public void playerVersusGhost(Player player, Ghost ghost) {
<b class="nc"><i class="no-highlight">77</i>&nbsp;        pointCalculator.collidedWithAGhost(player, ghost);</b>
<b class="nc"><i class="no-highlight">78</i>&nbsp;        player.setAlive(false);</b>
<b class="nc"><i class="no-highlight">79</i>&nbsp;        player.setKiller(ghost);</b>
<i class="no-highlight">80</i>&nbsp;    }
<i class="no-highlight">81</i>&nbsp;
<i class="no-highlight">82</i>&nbsp;    /**
<i class="no-highlight">83</i>&nbsp;     * Actual case of player consuming a pellet.
<i class="no-highlight">84</i>&nbsp;     *
<i class="no-highlight">85</i>&nbsp;     * @param player
<i class="no-highlight">86</i>&nbsp;     *           The player involved in the collision.
<i class="no-highlight">87</i>&nbsp;     * @param pellet
<i class="no-highlight">88</i>&nbsp;     *           The pellet involved in the collision.
<i class="no-highlight">89</i>&nbsp;     */
<i class="no-highlight">90</i>&nbsp;    public void playerVersusPellet(Player player, Pellet pellet) {
<b class="nc"><i class="no-highlight">91</i>&nbsp;        pointCalculator.consumedAPellet(player, pellet);</b>
<b class="nc"><i class="no-highlight">92</i>&nbsp;        pellet.leaveSquare();</b>
<i class="no-highlight">93</i>&nbsp;    }
<i class="no-highlight">94</i>&nbsp;
<i class="no-highlight">95</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
